/** @format */

import {
  editCompany,
  getCompanyUserDetail,
  getJobCount,
  getJobList,
  getJobDetail,
  getUserList,
  editJobStatus,
  companyEditUserJobType,
} from '@/api/company'
import {
  getUserDetail,
  createUserJob,
  getUserJobCount,
  getUserJobList,
  getUserTaskDetail,
  editUserJobType,
  createUserJobReport,
  editUserJobReport,
  editMobileUser,
  deleteUserJobReport,
  editRealName,
  getUserPayAuth,
  getWXConfig,
  getPayParams,
  applyRefund,
} from '@/api/user'

const myData = {
  namespaced: true,
  state: {
    company: '',
    companyUserDetail: {},
    userDetail: {},
    jobCount: 0,
    jobList: 0,
    jobDetail: {},
    userList: [],
    userJobCount: 0,
    userJobList: {},
    userTaskDetail: {},
    realName: 'bbbb',
    aaaa: 'aaaa',
    payParams: {},
  },
  actions: {
    async editCompany({ commit }, params) {
      let result = await editCompany(params)
      return result
    },
    async getCompanyUserDetail({ commit }) {
      let result = await getCompanyUserDetail()
      commit('setCompanyUserDetail', result)
      return result
    },
    async getUserDetail({ commit }) {
      let result = await getUserDetail()
      commit('setUserDetail', result)
    },
    async getJobCount({ commit, state }) {
      const companyId = state.companyUserDetail.zbCompanyResponse.id
      let result = await getJobCount({ companyId })
      commit('setJobCount', result)
    },
    async getJobList({ commit, state }) {
      const companyId = state.companyUserDetail.zbCompanyResponse.id
      let result = await getJobList({ companyId })
      commit('setJobList', result.content)
    },
    async getJobDetail({ commit }, jobId) {
      let result = await getJobDetail({ jobId })
      commit('setJobDetail', result)
    },
    async getUserList({ commit }, jobId) {
      let result = await getUserList({ jobId })
      commit('setUserList', result.content)
    },
    async editJobStatus({ commit }, { jobId, jobStatus }) {
      let result = await editJobStatus({ jobId, jobStatus })
      return result
    },
    async editMobileUser({ commit }, params) {
      let result = await editMobileUser(params)
      return result
    },
    async createUserJob({ commit }, jobId) {
      let result = await createUserJob({ jobId })
      return result
    },
    async getUserJobCount({ commit }) {
      let result = await getUserJobCount()
      commit('setUserJobCount', result)
    },
    async getUserJobList({ commit }) {
      let result = await getUserJobList()
      commit('setUserJobList', result)
    },
    async getUserTaskDetail({ commit }, userJobId) {
      let result = await getUserTaskDetail({ userJobId })
      result.jobResponse.userJobId = result.userJobId
      result.jobResponse.depositStatus = result.depositStatus
      result.jobResponse.userJobStatus = result.userJobStatus
      result.jobResponse.userJobStatusDesc = result.userJobStatusDesc //因为所有任务详情公用的一个页面，公司和个人接口返回数据格式不一致，页面需要展示userJobStatusDesc字段而此字段在userJobStatusDesc里
      commit('setUserTaskDetail', result)
    },
    async companyEditUserJobType({ commit }, { userJobId, userJobStatus }) {
      let result = await companyEditUserJobType({ userJobId, userJobStatus })
      return result
    },
    async editUserJobType({ commit }, { userJobId, userJobStatus }) {
      let result = await editUserJobType({ userJobId, userJobStatus })
      return result
    },
    async createUserJobReport({ commit }, params) {
      let result = await createUserJobReport(params)
      return result
    },
    async editUserJobReport({ commit }, params) {
      let result = await editUserJobReport(params)
      return result
    },
    async deleteUserJobReport({ commit }, params) {
      let result = await deleteUserJobReport(params)
      return result
    },
    async getUserJobList({ commit }) {
      let result = await getUserJobList()
      commit('setUserJobList', result)
    },
    async editRealName({ commit }, params) {
      let result = await editRealName(params)
      //   commit('setRealName', result)
      return result
    },
    async getUserPayAuth({ _ }, redirectUrl) {
      let result = await getUserPayAuth(redirectUrl)
      if (!result) return
      if (result.auth_required === 'true') {
        window.location.href = result.url
      }
      // console.log(result)
    },
    async getWXConfig({ _ }, jobId) {
      let configs = await getWXConfig(jobId)
      wx.config({
        debug: false,
        appId: configs.appId,
        nonceStr: configs.nonceStr,
        timestamp: configs.timestamp,
        signature: configs.signature,
        jsApiList: ["chooseWXPay"]
      })
    },
    async getPayParams({ commit }, jobId) {
      let result = await getPayParams(jobId)
      commit('setPayParams', result)
      // console.log(result)
    },
    async applyRefund({ _ }, userJobId) {
      await applyRefund(userJobId)
    },
  },
  mutations: {
    setCompanyUserDetail(state, companyUserDetail) {
      state.companyUserDetail = companyUserDetail
    },
    setUserDetail(state, userDetail) {
      state.userDetail = userDetail
    },
    setJobCount(state, { jobCount }) {
      state.jobCount = jobCount
    },
    setJobList(state, jobList) {
      state.jobList = jobList
    },
    setJobDetail(state, jobDetail) {
      state.jobDetail = jobDetail
    },
    setUserList(state, userList) {
      state.userList = userList
    },
    setUserJobCount(state, userJobCount) {
      state.userJobCount = userJobCount.userJobCount
    },
    setUserJobList(state, userJobList) {
      state.userJobList = userJobList
    },
    setPayParams(state, payParams) {
      state.payParams = payParams
    },
    setUserTaskDetail(state, userTaskDetail) {
      state.userTaskDetail = userTaskDetail.jobResponse
    }
    // setRealName(state, realName) {
    //   state.realName = realName
    // }

    // setUserJobReportResponse(state, userJobReportResponse) {
    //   state.userJobReportResponse = userJobReportResponse
    // },
  }
}

export default myData
